package tt.dz.order.pojo;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import tt.dz.order.dao.BaseDAOUtil;
import tt.dz.order.exception.BusinessException;
import tt.dz.order.util.AmountUtil;
import tt.dz.order.util.OrderConstant.*;
import tt.dz.order.vo.OrderHistoryQueryVO.OrderHistoryQueryParamVO;
import tt.dz.order.vo.UserPayInfoVO;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: 订单基本信息
 * @author zhoutian
 * @date 2015-11-26
 */
public class OrderPayInfo {
	// 订单号:1位用户类型+3位渠道号+14位当前时间+4位随机数
	private String orderNo;
	  
	// 用户Id
	private String userId;
	private String userName;
	// 运营商号
	private String merchantCode;
	private String merchantName;
	private BigDecimal caculatedTotalPrice;
	 
	// 支付类型 1 第三方实时支付 2 统付 3 企业支付 4 电子钱包支付 5 代付 6 线下卡
	private Integer payTypeCode;
	// 支付渠道编码,见OrderConstant.OrderPayChannelCode
	private String payChannelCode;
	// 第三方交易流水号
	private String thdPaySeq;
	// 第三方转账金额
	private BigDecimal thdPayAmount;
	// 第三方手续费
	private BigDecimal thdPoundage;
	// 第三方转账完成时间
	private String thdEndTime;
	// 第三方交易状态码
	private String tradeStatusCode;
	private String statusName;
	// 订单状态
	private Integer status;
	// 创建时间
	private Date createTime;
	  

	public OrderPayInfo() {
		this.status = OrderStatus.unPay.value();
		this.createTime = new Date();
		this.caculatedTotalPrice = new BigDecimal("0.00");
	}

	private static final String select_all_sql = "select orderNo,userId,merchantCode,caculatedTotalPrice,payTypeCode,payChannelCode,thdPaySeq,thdPayAmount,thdPoundage,thdEndTime,tradeStatusCode,status,createTime from order_pay_info";

	private static final String update_all_sql = "update order_pay_info set orderNo=:orderNo, userId=:userId, merchantCode=:merchantCode, caculatedTotalPrice=:caculatedTotalPrice, payTypeCode=:payTypeCode, payChannelCode=:payChannelCode, thdPaySeq=:thdPaySeq, thdPayAmount=:thdPayAmount, thdPoundage=:thdPoundage, thdEndTime=:thdEndTime, tradeStatusCode=:tradeStatusCode, status=:status, createTime=:createTime";

	public void save() {
		String sql = "insert into order_pay_info(orderNo,userId,merchantCode,caculatedTotalPrice,payTypeCode,payChannelCode,thdPaySeq,thdPayAmount,thdPoundage,thdEndTime,tradeStatusCode,status,createTime) values(:orderNo, :userId, :merchantCode, :caculatedTotalPrice, :payTypeCode, :payChannelCode, :thdPaySeq, :thdPayAmount, :thdPoundage, :thdEndTime, :tradeStatusCode, :status, :createTime)";
		BaseDAOUtil.updateObject(sql, this);
	}

	public void update() {
		String sql = update_all_sql + " where orderNo = :orderNo";
		BaseDAOUtil.updateObject(sql, this);
	}
	public void updateOrderHide() {
		String sql ="update order_pay_info set hide=:hide where orderNo = :orderNo";
		BaseDAOUtil.updateObject(sql, this);
	}
	public void updateOrderAttach() {
		String sql ="update order_pay_info set attach=:attach where orderNo = :orderNo";
		BaseDAOUtil.updateObject(sql, this);
	}

	/**
	 * 根据订单号查询订单信息
	 * 
	 * @param orderNo
	 *            订单号
	 * @return 订单信息
	 */
	public static OrderPayInfo getById(String orderNo) {
		String sql = select_all_sql + " where orderNo = :orderNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNo", orderNo);
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderPayInfo.class);
	}


	/**
	 * 根据关联订单号以及阶段号获取对应的订单 <br>
	 * 关联订单号+阶段,是唯一性的
	 * 
	 * @param relateNo
	 *            关联订单号
	 * @param stage
	 *            阶段
	 * @return
	 */
	public static OrderPayInfo getOrderInfoByOrderNo(String orderNo) {
		String sql = select_all_sql + " where orderNo = :orderNo";
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("orderNo", orderNo);
		return BaseDAOUtil.queryForObject(sql, paramMap, OrderPayInfo.class);
	}


	/**
	 * 收到支付宝支付回调时修改订单支付信息
	 * 
	 * @param userPayInfoVO
	 *            用户支付信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void updateOrderAfterALiPayBack(AliPayCallBackRecord aliPayCallBackRecord) {
		String sql = "update order_pay_info set  payChannelCode=:payChannelCode,  payTypeCode =:payTypeCode, status = :status, thdPaySeq = :thdPaySeq, thdPayAmount = :thdPayAmount, thdPoundage = :thdPoundage, thdEndTime = :thdEndTime, tradeStatusCode = :tradeStatusCode where orderNo = :orderNo";
		Map paramMap = new HashMap();
		paramMap.put("status", OrderStatus.payedDone.value());
		paramMap.put("thdPaySeq", aliPayCallBackRecord.getTradeNo());
		paramMap.put("thdPayAmount", new BigDecimal(aliPayCallBackRecord.getTotalFee()));
		paramMap.put("thdPoundage", new BigDecimal("0.00"));
		paramMap.put("thdEndTime", aliPayCallBackRecord.getGmtPayment());
		paramMap.put("tradeStatusCode", aliPayCallBackRecord.getTradeStatus());
		paramMap.put("orderNo", aliPayCallBackRecord.getOrderNo());
		// 当前已经确认了是第三发支付方式
		paramMap.put("payTypeCode", OrderPayWay.thirdImmediately.value());
		// 支付渠道已经确认了是支付宝支付方式，payChannelCode
		paramMap.put("payChannelCode", OrderPayChannelCode.alipay.value());
		BaseDAOUtil.update(sql, paramMap);
	}
 
	/**
	 * 收到微信支付回调时修改订单支付信息
	 * 
	 * @param userPayInfoVO
	 *            用户支付信息
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void updateOrderAfterWeiXinPayBack(WeiXinPayCallBackRecord weixinPayCallBackRecord) {
		String sql = "update order_pay_info set  payChannelCode=:payChannelCode,  payTypeCode =:payTypeCode,  status = :status, thdPaySeq = :thdPaySeq, thdPayAmount = :thdPayAmount, thdPoundage = :thdPoundage, thdEndTime = :thdEndTime, tradeStatusCode = :tradeStatusCode where orderNo = :orderNo";
		Map paramMap = new HashMap();
		paramMap.put("status", OrderStatus.payedDone.value());
		paramMap.put("thdPaySeq", weixinPayCallBackRecord.getTradeNo());
		// 微信支付回调的totalFee单位为分，需要转化为元
		paramMap.put("thdPayAmount",
				AmountUtil.divide(new BigDecimal(weixinPayCallBackRecord.getTotalFee()), new BigDecimal("100")));
		paramMap.put("thdPoundage", new BigDecimal("0.00"));
		paramMap.put("thdEndTime", weixinPayCallBackRecord.getTimeEnd());
		paramMap.put("tradeStatusCode", weixinPayCallBackRecord.getResultCode());
		paramMap.put("orderNo", weixinPayCallBackRecord.getOrderNo());
		// 当前已经确认了是第三发支付方式
		paramMap.put("payTypeCode", OrderPayWay.thirdImmediately.value());
		// 支付渠道已经确认了是微信支付方式，payChannelCode
		paramMap.put("payChannelCode", OrderPayChannelCode.weixinpay.value());
		BaseDAOUtil.update(sql, paramMap);
	}

	public String getOrderNo() {
		return orderNo;
	}

	public void setOrderNo(String orderNo) {
		this.orderNo = orderNo;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getMerchantCode() {
		return merchantCode;
	}

	public void setMerchantCode(String merchantCode) {
		this.merchantCode = merchantCode;
	}

	public String getMerchantName() {
		return merchantName;
	}

	public void setMerchantName(String merchantName) {
		this.merchantName = merchantName;
	}

	public BigDecimal getCaculatedTotalPrice() {
		return caculatedTotalPrice;
	}

	public void setCaculatedTotalPrice(BigDecimal caculatedTotalPrice) {
		this.caculatedTotalPrice = caculatedTotalPrice;
	}

	public Integer getPayTypeCode() {
		return payTypeCode;
	}

	public void setPayTypeCode(Integer payTypeCode) {
		this.payTypeCode = payTypeCode;
	}

	public String getPayChannelCode() {
		return payChannelCode;
	}

	public void setPayChannelCode(String payChannelCode) {
		this.payChannelCode = payChannelCode;
	}

	public String getThdPaySeq() {
		return thdPaySeq;
	}

	public void setThdPaySeq(String thdPaySeq) {
		this.thdPaySeq = thdPaySeq;
	}

	public BigDecimal getThdPayAmount() {
		return thdPayAmount;
	}

	public void setThdPayAmount(BigDecimal thdPayAmount) {
		this.thdPayAmount = thdPayAmount;
	}

	public BigDecimal getThdPoundage() {
		return thdPoundage;
	}

	public void setThdPoundage(BigDecimal thdPoundage) {
		this.thdPoundage = thdPoundage;
	}

	public String getThdEndTime() {
		return thdEndTime;
	}

	public void setThdEndTime(String thdEndTime) {
		this.thdEndTime = thdEndTime;
	}

	public String getTradeStatusCode() {
		return tradeStatusCode;
	}

	public void setTradeStatusCode(String tradeStatusCode) {
		this.tradeStatusCode = tradeStatusCode;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public String getStatusName() {
		return statusName;
	}

	public void setStatusName(String statusName) {
		this.statusName = statusName;
	}
}
